package com.xg.controller;

import com.xg.common.constant.CommonStatusEnum;
import com.xg.common.response.UsedCarType;
import com.xg.common.result.ApiResult;
import com.xg.common.util.Func;
import com.xg.common.util.PageUtil;
import com.xg.service.UsedCarTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 车型处理
 */
@RequestMapping("/usedCarType")
@RestController
public class UsedCarTypeController {

    @Autowired
    private UsedCarTypeService usedCarTypeService;

    /**
     * 车型分页查询
     * @param params
     * @return
     */
    @RequestMapping("/list")
    public ApiResult<PageUtil<UsedCarType>> list(@RequestParam Map<String , Object> params){
        PageUtil<UsedCarType> pageUtils = usedCarTypeService.list(params);
        return ApiResult.success(pageUtils);
    }

    /**
     * 车型列表
     */
    @GetMapping("/usedTypeList")
    public ApiResult<List<UsedCarType>> queyUsedTypeList(){
        return usedCarTypeService.queryUsedTypeList();
    }


    /**
     * 车型删除结合批量和单个删除
     */
    @RequestMapping("/delete")
    public ApiResult deleteType(@RequestBody List<Integer> ids){
        if (!Func.isEmpty(ids)) {
            return  usedCarTypeService.deleteType(ids);
        }
        return ApiResult.error(CommonStatusEnum.TYPE_DELETE_ERROR.getCode(), CommonStatusEnum.TYPE_DELETE_ERROR.getMsg());
    }

    /**
     * 查询单个车型信息
     */
    @RequestMapping("/info/{typeId}")
    public ApiResult<UsedCarType> selectTypeInfo(@PathVariable("typeId") Integer typeId){
        if (!Func.isEmpty(typeId)) {
            return usedCarTypeService.selectTypeInfo(typeId);
        }
        return ApiResult.error(CommonStatusEnum.TYPE_INFO_ERROR.getCode(), CommonStatusEnum.TYPE_INFO_ERROR.getMsg());
    }


    /**
     * 品牌信息新增和修改
     */
    @RequestMapping("/save")
    public ApiResult saveType(@RequestBody UsedCarType usedCarType){
        if (!Func.isEmpty(usedCarType)) {
            return usedCarTypeService.saveType(usedCarType);
        }
        return ApiResult.error(CommonStatusEnum.TYPE_SAVE_ERROR.getCode(), CommonStatusEnum.TYPE_SAVE_ERROR.getMsg());
    }
}
